package com.cmq.web_1.filter;


import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import java.io.IOException;
import java.util.Date;


public class PerformanceFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        long startTime = new Date().getTime();
        request.setAttribute("startTime", startTime); // 存储开始时间

        chain.doFilter(request, response); // 调用下一个过滤器

        long endTime = new Date().getTime();
        long elapsedTime = endTime - startTime;
        request.setAttribute("elapsedTime", elapsedTime); // 存储耗时，供日志过滤器使用
        System.out.println("【性能监控】请求总耗时：" + elapsedTime + " 毫秒");
    }

    @Override
    public void destroy() {}
}